//
//  ApiUserBusiness.m
//  KangarooDoctor
//
//  Created by 廖罡 on 2024/1/19.
//

#import "ApiUserBusiness.h"
#import "DataModel.h"


NSString *kUserPhone = @"kUserPhone";
NSString *kUserToken = @"kUserToken";

@implementation ApiUserBusiness

+(instancetype)shared
{
    static ApiUserBusiness *u = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        u = [[ApiUserBusiness alloc]init];
    });
    return u;
}


-(void)getUserAgreement:(ApiCallback)callback
{
    [[Api shared] postAtPath:@"/api/app/PersonalCenter/getData" param: nil header:@{@"token":self.token} callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
            NSLog(@"fail: %@", err);
           callback(nil, err);
        }else{
            NSLog(@"ok, %@", result);
            
            NSDictionary *dict = result;
            
            GDVerifyPhoneRoot *root = [[GDVerifyPhoneRoot alloc] initWithDictionary:dict];
            
            callback(root, nil);
        }
    }];
}

-(void)bindingBodyTempWithImei:(NSString*)imei cb:(ApiCallback)callback
{
    NSDictionary *param = @{@"deviceNumber":imei};
    
    [[Api shared] postAtPath:@"/api/app/DeviceManagement/binding"
                       param: param
                      header:@{@"token":self.token} callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
            NSLog(@"fail: %@", err);
           callback(nil, err);
        }else{
            NSLog(@"ok, %@", result);
            callback(result, nil);
        }
    }];
}



-(void)getDeviceListWithParams:(NSDictionary * ) params  callback:(ApiCallback)callback
{
    NSLog(@"getDeviceList.....");
    [[Api shared] getAtPath:@"/api/app/DeviceManagement/list"
                       param: nil
                     header:@{@"token": self.token}
                    callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
            NSLog(@"fail: %@", err);
           callback(nil, err);
        }else{
            NSLog(@"ok, %@", result);
            callback(result, nil);
        }
    }];
}

-(void)getDeviceDetail:(NSString*)deviceId cb:(ApiCallback)callback
{
    [[Api shared] getAtPath:[NSString stringWithFormat:@"/pressures/detail/%@", deviceId]
                       param:@{@"deviceId": deviceId}
                      header:@{@"token":self.token}
                    callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
            NSLog(@"fail: %@", err);
           callback(nil, err);
        }else{
            NSLog(@"ok, %@", result);
            callback(result, nil);
        }
    }];

}

-(void)getDeviceHighTemp:(NSString*)did cb:(ApiCallback)callback
{
     [[Api shared] getAtPath:@"/api/app/DeviceManagement/temperature"
                       param:@{@"deviceId": did}
                      header:@{@"token":self.token}
                    callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
            NSLog(@"fail: %@", err);
           callback(nil, err);
        }else{
            NSLog(@"ok, %@", result);
            
            callback(result, nil);
        }
    }];

   
}

//0 ：今日，1：近三天，2：近一周
-(void)getDeviceTempCurve:(NSString*)did figure:(NSString*)figure cb:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/api/app/DeviceManagement/curve"
                      param:@{
        @"deviceId": did,
        @"figure":figure}
                     header:@{@"token":self.token}
                   callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
           NSLog(@"fail: %@", err);
           callback(nil, err);
       }else{
           NSLog(@"ok, %@", result);
           
           NSArray *arr = result;
           
           NSMutableArray<TempCurveItem*> *arr2 = [NSMutableArray array];
           for (NSDictionary *dict in arr) {
               TempCurveItem *item = [[TempCurveItem alloc] initWithDictionary:dict];
               [arr2 addObject:item];
           }
           
           callback(arr2, nil);
       }
   }];
}

/**
    获取一周内血压计测量数据
 **/
-(void) getAllBpDataByDeviceIdForWeek:(NSDictionary *) params cb:(ApiCallback)callback
{
    [[Api shared] getAtPath:[NSString stringWithFormat:@"/pressures/device/oneweeklist/%@", params[@"deviceId"]]
                      param: params
        header:@{@"token":self.token}
        callback:^(id  _Nonnull result, NSString * _Nonnull err) {
        if (err) {
           NSLog(@"fail: %@", err);
           callback(nil, err);
       }else{
           NSLog(@"ok, %@", result);
           callback(result, nil);
       }
   }];
}


-(void)getMyProfile:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/users" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            GDUserInfo *info = [[GDUserInfo alloc] initWithDictionary:result];
            callback(info, nil);
        }
    }];
}



-(void)updateMyProfile:(NSDictionary*)dict cb:(ApiCallback)callback
{
    [[Api shared] method:@"PUT" path:@"/users" param:dict header:@{@"token":self.token} callback:callback];
}


-(void)getOssAddress:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/servicesupport/oss/policy" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}



-(void)getPressHistory:(NSString*)from to:(NSString*)to cb:(ApiCallback)callback
{
    NSDictionary *param = @{
        @"startDate":from,
        @"endDate":to,
    };
    [[Api shared] getAtPath:@"/pressures/list" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


-(void)updatePressData:(int)highPress
                   low:(int)lowPress
                 pulse:(int)pulse
              deviceId:(NSString *) deviceId
                    cb:(ApiCallback)callback
{
    NSDictionary *param = @{
        @"highPressure": @(highPress),
        @"lowPressure": @(lowPress),
        @"pulse": @(pulse),
        @"deviceId": [NSString stringWithFormat:@"%@", deviceId]
    };
    
    [[Api shared] postAtPath:@"/pressures" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}

- ( void ) getBpDeviceMeasureDataByDeviceId:(NSDictionary *) params callback:(ApiCallback)callback {
    
    NSString * uri = [NSString stringWithFormat:@"/pressures/device/pagination/%@", params[@"deviceId"]];
    [[Api shared] getAtPath:uri param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


-(void)updateDevice:(GDDevice*)device cb:(ApiCallback)callback
{
    NSDictionary *param = [device toDictionary];
    
    [[Api shared] postAtPath:@"/api/app/DeviceManagement/update" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}

-(void)deleteDevice:(GDDevice*)device cb:(ApiCallback)callback
{
    NSDictionary *param = @{
        @"deviceId": device.idField,
    };
    
    [[Api shared] method:@"DELETE" path:@"/api/app/PersonalCenter/delete" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


//上传数据到后台
- ( void ) updateBpDeviceDataToService:(NSDictionary *) param  cb:(ApiCallback)callback{
    [[Api shared] postAtPath:@"/pressures" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


-(void)deleteSelfAccount:(NSString*)pinCode cb:(ApiCallback)callback
{
    NSDictionary *param = @{
        @"code": pinCode,
    };
    
    [[Api shared] method:@"DELETE" path:@"/users" param:param header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}

-(void)getBannerList:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/banners/list" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


//============================ 帐号 ========================================
/***
    1、帐号登录
 */
- ( void ) loginByAccountPwd:(NSString*) username pwd:(NSString *) pwd  callback:(ApiCallback)callback {
    
    NSDictionary *param = @{
        @"userName": username,
        @"password": pwd
    };
    [[Api shared] postAtPath:@"/auth/login/pwd" param:param header:@{@"token":@""} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            
            NSLog(@"帐号登录成功.....");
            callback(result, nil);
        }
    }];
}


/***
    2、验证码登录
 */
- ( void ) loginByValidateCode:(NSString*) username code:(NSString *) code  callback:(ApiCallback)callback {
    
    NSDictionary *param = @{
        @"userName": username,
        @"code": code
    };
    [[Api shared] postAtPath:@"/auth/login/code" param:param header:@{@"token":@""} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


/***
    3、第三方登录
 */
- ( void ) loginByWechat:(ApiCallback)callback {
    [[Api shared] postAtPath:@"/auth/login/microwechat" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


/***
    4、重置密码
 */
- ( void ) resetPasswordWithParams:(NSDictionary *) params callback :(ApiCallback)callback {
    [[Api shared] putAtPath:@"/auth/resetPassword" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


/***
    5、注册帐号
 */
- ( void ) registerAccountWithParams:params callback:(ApiCallback)callback {
    [[Api shared] postAtPath:@"/auth/register" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
    
}

/**
    6、手机号绑定微信
 */
- ( void ) wechatBindPhone:(ApiCallback) callback {
    [[Api shared] getAtPath:@"/auth/wechat/bindPhone" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


/***
  7、注销账号
 */
- ( void ) logoutAccountWithParams: (NSDictionary *) params callback:(ApiCallback) callback {
    [[Api shared] deleteAtPath:@"/users" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


//用户反馈
- ( void ) addFeedBackWithParams: (NSDictionary *) params callback:(ApiCallback) callback {
    
    NSLog(@"token: %@", self.token);
    [[Api shared] postAtPath:@"/systems/feedback" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
    
}


//============================= 资讯 =======================================

//获取资讯列表
- ( void ) getNewsPaginatonWithParams: (NSDictionary *) params callback:(ApiCallback)callback {
    [[Api shared] getAtPath:@"/cmsTextNewss/pagination" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}



//获取资讯详情
- ( void ) getNewsDetailWithParams: (NSDictionary *) params callback:(ApiCallback)callback {
    [[Api shared] getAtPath:[NSString stringWithFormat:@"/cmsTextNewss/%@",params[@"id"]] param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}




//============================= 设备 =======================================
/***
    获取设备列表
 */
- ( void ) getDevicePagination:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/api/app/DeviceManagement/list" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}
/***
   获取设备详情
 */
- ( void ) getDeviceDetailById:(ApiCallback)callback
{
    [[Api shared] getAtPath:@"/api/app/DeviceManagement/particulars" param:nil header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}

/**
  绑定设备
 **/
- ( void ) bindDeviceWithParams: (NSDictionary *) params callback:(ApiCallback)callback {
    [[Api shared] postAtPath:@"/api/app/DeviceManagement/binding" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}

/***
  是否绑定设备
 */
- ( void ) isbindedDeviceWithParams: (NSDictionary *) params callback:(ApiCallback)callback {
    [[Api shared] getAtPath:@"/api/app/DeviceManagement/isBinded" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


/***
    解绑设备
 */
- ( void ) unbindDeviceWithParams: (NSDictionary *) params callback:(ApiCallback)callback {
    [[Api shared] deleteAtPath:@"/api/app/PersonalCenter/delete" param:params header:@{@"token":self.token} callback:^(id  _Nullable result, NSString * _Nullable err) {
        if (err) {
            callback(nil, err);
        } else {
            callback(result, nil);
        }
    }];
}


@end
